﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;


public partial class sysadmin_QuestionManager_AddPaperInfo : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerPaperInfo manager = new ManagerPaperInfo();

    ManagerPaper objManagerPaper = new ManagerPaper();
    //ManagerUserInfo userManager = new ManagerUserInfo();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.AddPaper))
        //{
        //    Script.ShowAndHistory("您没有添加试卷的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            ViewState["UserID"] = 10000;
            InitPage();
            //BinderDDL();
        }
    }

    //private void BinderDDL()
    //{
    //    DataTable dt = new DataTable();
    //    dt = userManager.Query("");
    //    ddlUserList.DataSource = dt;
    //    ddlUserList.DataTextField = "UserName";
    //    ddlUserList.DataValueField = "UserID";
    //    ddlUserList.DataBind();
    //}

    protected override void InitPage()
    {
        lbl_AddPaperInfo_Title.Text = GetLanageValueByKey("lab_AddPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_PaperName.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_PaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_DefaultScore.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperDefaultPassScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_Diffculty.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_SelectPaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddPaperInfo_TestTime.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperTestTime", PowerCreator.LMS.Language.LanagetItemType.Page);

        rdbtn_Defoult.Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_Test.Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddPaperInfo_SaveNext.Text = GetLanageValueByKey("btn_AddPaperInfo_SavePaperAndSelect", PowerCreator.LMS.Language.LanagetItemType.Page);

        rdbtn_Defoult.Text = GetLanageValueByKey("lab_AddPaperInfo_CommonPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_Test.Text = GetLanageValueByKey("lab_AddPaperInfo_TestPaper", PowerCreator.LMS.Language.LanagetItemType.Page);

        ddlQuestionSelectType.Items[0].Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType1", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionSelectType.Items[1].Text = GetLanageValueByKey("Msg_AddPaperInfo_SelectQuestionType2", PowerCreator.LMS.Language.LanagetItemType.Page);

        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("Msg_AddPaperInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_PassScore.ErrorMessage = GetLanageValueByKey("Msg_AddPaperInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_TestTime.ErrorMessage = GetLanageValueByKey("Msg_AddPaperInfo_checkmsg3", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();
    }
    private bool PageCheck()
    {
        if (rdbtn_Defoult.Checked)
        {
            if (string.IsNullOrEmpty(txtPaperName.Text.Trim()) | string.IsNullOrEmpty(txtPassScore.Text.Trim()) | string.IsNullOrEmpty(txtTestTime.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }
            if (!PageValidate.IsNumber(txtPassScore.Text.Trim()) | !PageValidate.IsNumber(txtTestTime.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg5", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }

        }
        else
        {
            if (string.IsNullOrEmpty(txtPaperName.Text.Trim()))
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg1", PowerCreator.LMS.Language.LanagetItemType.Page));
                return false;
            }
            if (!string.IsNullOrEmpty(txtPassScore.Text.Trim()))
            {
                if (!PageValidate.IsNumber(txtPassScore.Text.Trim()))
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
            }
        }
        string search = " PaperName=" + "'" + txtPaperName.Text + "'";
        if (objManagerPaper.CountPaper(search) != 0)
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_AddPaperInfo_checkmsg6", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;

        }
        return true;

    }
    protected void btnSaveNext_Click(object sender, EventArgs e)
    {
        //保存并下一步


        if (!PageCheck())
        {
            return;
        }

        VOPaper info = new VOPaper();
        info.PaperCreateID = int.Parse(ViewState["UserID"].ToString());
        info.PaperCreateTime = DateTime.Now;
        if (txtTestTime.Text.Trim() == "")
        {
            info.PaperPassScore = 0;
        }
        else
        {
            info.PaperPassScore = int.Parse(txtPassScore.Text);
        }
        info.PaperShowAnswer = 2;
        info.PaperShowScore = 2;
        info.PaperMarkID = int.Parse(ViewState["UserID"].ToString());
        info.PaperName = txtPaperName.Text;
        info.PaperSelectType = int.Parse(ddlQuestionSelectType.SelectedValue);

        info.PaperCelueID = -1;

        if (rdbtn_Defoult.Checked)
        {
            info.PaperTestTime = int.Parse(txtTestTime.Text);
            info.PaperType = 0;
        }
        else
        {
            info.PaperTestTime = 0;
            info.PaperType = 1;
        }
        info.PaperBuildState = 0;
        
        info.PaperScore = 0;
                
        int pid = 0;
        objManagerPaper.CreatePaper(info,ref pid);
        if (pid != 0)
            ViewState.Add("PaperID",pid.ToString());

        string url =string.Empty;
        int urltype = int.Parse(ddlQuestionSelectType.SelectedValue);
        if (urltype == 1)
        {
            url = "SelectQuestion.aspx?p=" + ViewState["PaperID"].ToString() + "&type=" + urltype.ToString();
        }
        else if (urltype == 2)
        {
            url = "CelueSelectQuestion.aspx?p=" + ViewState["PaperID"].ToString() + "&type=" + urltype.ToString();
        }
        Script.RedirectURL(url);
    }

    protected void rdbtn_Defoult_CheckedChanged(object sender, EventArgs e)
    {
        txtTestTime.Enabled = true;
        lab_TestTimeMsg.Visible = true;
        rfv_TestTime.Enabled = true;
        txtPassScore.Enabled = true;
        txtTestTime.BackColor = System.Drawing.Color.White;
        txtPassScore.BackColor = System.Drawing.Color.White;
    }
    protected void rdbtn_Test_CheckedChanged(object sender, EventArgs e)
    {
        txtTestTime.Enabled = false;
        lab_TestTimeMsg.Visible = false;
        rfv_TestTime.Enabled = false;
        txtTestTime.BackColor = System.Drawing.Color.LightGray;
        txtPassScore.BackColor = System.Drawing.Color.LightGray;
        txtPassScore.Enabled = false;
        txtPassScore.Text = "0";
        txtTestTime.Text = "0";
        
    }
}
